home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / libmailtools-perl / demos / send_demo < prev   
Encoding:
Text File  |  2008-11-04  |  1.5 KB  |  87 lines

  1. #!/usr/bin/perl -w
  2. require Mail::Internet;
  3. require Mail::Alias;
  4. use Getopt::Long;
  5.  
  6. sub expand_aliases 
  7. {
  8.  my $mail = shift;
  9.  my $aliasfile = $ENV{HOME} . "/.mailrc";
  10.  my($tag,$id);
  11.  
  12.  if( -f $aliasfile )
  13.   {
  14.    my $alias = Mail::Alias::Binmail->new($aliasfile);
  15.    # Expand aliases
  16.  
  17.    foreach $tag (qw(To Cc Bcc)) 
  18.     {
  19.      @ids = ();
  20.      foreach $id (Mail::Address->parse($mail->get($tag)))
  21.       {
  22.        my $addr = $id->address;
  23.        my @expn = $alias->expand($addr);
  24.        if(scalar(@expn) == 1)
  25.         {
  26.          $id->address($expn[0]);
  27.          push(@ids,$id->format);
  28.         }
  29.        else
  30.         {
  31.          push(@ids,@expn);
  32.         }
  33.       }
  34.      $mail->combine($tag,',');
  35.      $mail->replace($tag, join(", ", @ids));
  36.     }
  37.   }
  38. }
  39.  
  40.  
  41. ###
  42. ### Main program
  43. ###
  44.  
  45. GetOptions(qw(post nosig v));
  46.  
  47. $opt_post = 1 if $0 =~ m#(\A|/)post\Z#;
  48.  
  49. $verbose = defined $opt_v && $opt_v ? 1 : 0;
  50. $posting = defined $opt_post && $opt_post ? 1 : 0;
  51. $sign    = defined $opt_nosig && $opt_nosig ? 0 : 1;
  52.  
  53. $mail =  Mail::Internet->new([ <> ]);
  54.  
  55. expand_aliases($mail);
  56.  
  57. $mail->add_signature if($sign);
  58.  
  59. if($posting)
  60.  {
  61.   my @groups = $mail->nntppost();
  62.  
  63.   if($verbose && @groups)
  64.    {
  65.     $groups = "Newsgroups: " . join(", ", @groups);
  66.     $groups =~ s/(.{10,78}),/$1\n/g if(length($groups) > 78);
  67.     print $groups,"\n";
  68.    }
  69.  }
  70. else
  71.  {
  72.   $mail->delete('Newsgroups');
  73.  }
  74.  
  75. @recp = $mail->smtpsend();
  76.  
  77. if($verbose && @recp)
  78.  {
  79.   $recp = "Recipients: " . join(", ", @recp);
  80.   $recp =~ s/(.{10,78}),/$1\n/g if(length($recp) > 78);
  81.   print $recp,"\n";
  82.  }
  83.  
  84. exit;
  85.  
  86.  
  87.